window.onload = function () {
    document.getElementById("administrative").className = "add_class";
    document.getElementById("vehicle-information").className = "add-nav-active";
    document.getElementById("gebruikersrol_ul").style.display = "inline-block";
    document.getElementById("jiantou1").className = "iconfont icon-jiantou-xia";
    document.getElementById("organizational_management_ul").style.display = "none";
    upload_car_file()
};

function click_ul() {
    if (document.getElementById("jiantou").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou").className = "iconfont icon-jiantou-xia";
        document.getElementById("organizational_management_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou").className = "iconfont icon-jiantou-shang";
        document.getElementById("organizational_management_ul").style.display = "none";
    }
}

function click_ul1() {
    if (document.getElementById("jiantou1").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou1").className = "iconfont icon-jiantou-xia";
        document.getElementById("gebruikersrol_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou1").className = "iconfont icon-jiantou-shang";
        document.getElementById("gebruikersrol_ul").style.display = "none";
    }
}

function click_ul2() {
    if (document.getElementById("jiantou2").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou2").className = "iconfont icon-jiantou-xia";
        document.getElementById("meeting_management_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou2").className = "iconfont icon-jiantou-shang";
        document.getElementById("meeting_management_ul").style.display = "none";
    }
}

function click_ul3() {
    if (document.getElementById("jiantou3").className === "iconfont icon-jiantou-shang"){
        document.getElementById("jiantou3").className = "iconfont icon-jiantou-xia";
        document.getElementById("issue_approval_ul").style.display = "inline-block";
    }else {
        document.getElementById("jiantou3").className = "iconfont icon-jiantou-shang";
        document.getElementById("issue_approval_ul").style.display = "none";
    }
}


// 图片上传
function Upload_img() {
    var form = new FormData();
    var files = document.getElementById("myfile").files[0];
    form.append("file", files);

    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/announcements/upload_imgs", true);
    xhr.send(form);
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                document.getElementById("cover-size").src = JsonDate["data"][0]["img-url"];
                document.getElementById("cover-img").style.display = "block";
            }
        }
    };
}


// 上传附件
function upload_accessory() {
    var form = new FormData();
    var files = document.getElementById("accessory").files[0];
    form.append("file", files);

    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/announcements/upload_accessory", true);
    xhr.send(form);
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                creat_table(JsonDate["data"][0])
            }
        }
    };
}


// 创建附件列表
function creat_table(JsonData) {
    var t_table = document.getElementById("t_table");
    var tr = document.createElement("tr");

    var td = document.createElement("td");var td1 = document.createElement("td");var td2 = document.createElement("td");var td3 = document.createElement("td");var td4 = document.createElement("td");var td5 = document.createElement("td");var td6 = document.createElement("td");
    td.innerHTML = "<input id='"+JsonData["accessory_name"]+"' class='check_box' type=\"checkbox\">";td.style.textAlign = "center";
    td1.innerHTML = t_table.children.length+1;td1.style.textAlign = "center";
    td2.innerHTML = JsonData["accessory_name"];td2.style.paddingLeft = "1%";
    td3.innerHTML = JsonData["file_size"];td3.style.paddingLeft = "1%";
    td4.innerHTML = JsonData["upload_time"];td4.style.textAlign = "center";
    td5.innerHTML = "";
    var local_host = window.location.host;
    td6.innerHTML = "<a href='"+"/announcements/file_download?file_path=" + JsonData["accessory_url"] + "'>下载</a>";td6.style.textAlign = "center";
    tr.appendChild(td);tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);tr.appendChild(td4);tr.appendChild(td5);tr.appendChild(td6);
    t_table.appendChild(tr);
}



// 获取新建新闻内容
function modify_car_content() {
    document.getElementById("loading").style.display = "inline-block";
    // var editor_html = editor.txt.html().replaceAll("&", "%26");                // 富文本内容
    var car_id = document.getElementById("car_id").innerHTML;         // 新闻名称
    var car_license_plate = document.getElementById("car_license_plate").value;         // 新闻名称
    var car_model = document.getElementById("car_model").value;                //公告日期
    var car_driver = document.getElementById("car_driver").value;                //公告日期
    var car_type = document.getElementById("car_type").value;                //公告日期
    var car_color = document.getElementById("car_color").value;                //公告日期
    var purchase_date = document.getElementById("purchase_date").value;                //公告日期
    var car_body_number = document.getElementById("car_body_number").value;                //公告日期
    var engine_number = document.getElementById("engine_number").value;                //公告日期
    var initial_inspection_date = document.getElementById("initial_inspection_date").value;                //公告日期
    var car_status = document.getElementById("car_status").value;                //公告日期
    var marks = document.getElementById("marks").value;                //公告日期
    var coverImg = document.getElementById("cover-size").src;
    var tr_tag = document.getElementsByTagName("tr");

    var file_list = new Array();
    for (var i=1; i<tr_tag.length; i++) {
        var file_dict = {};
        file_dict["file_name"] = tr_tag[i].children[2].innerHTML;
        file_dict["file_path"] = tr_tag[i].children[6].children[0].href;
        file_list.push(file_dict);
    }
    var sent_json = {};
    sent_json["car_id"] = String(car_id);
    sent_json["car_license_plate"] = String(car_license_plate);
    sent_json["car_model"] = String(car_model);
    sent_json["car_driver"] = String(car_driver);
    sent_json["car_type"] = String(car_type);
    sent_json["car_color"] = String(car_color);
    sent_json["purchase_date"] = String(purchase_date);
    sent_json["car_body_number"] = String(car_body_number);
    sent_json["engine_number"] = String(engine_number);
    sent_json["initial_inspection_date"] = String(initial_inspection_date);
    sent_json["engine_number"] = String(engine_number);
    sent_json["car_status"] = String(car_status);
    sent_json["marks"] = String(marks);
    sent_json["coverImg"] = coverImg;
    sent_json["file_list"] = file_list;
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/administrative/modify_cars.html", true);
    xhr.send(JSON.stringify(sent_json));
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var json_data = JSON.parse(xhr.responseText);
                alert(json_data['message'])

            }
        }
    };
}


function Confirm_Preview() {
    var newWindow = window.open("Preview.html", "Preview.html");
    var form = document.createElement("form");
}


function deltr(){
    var tb = document.getElementById('t_table');
    var rowNum=tb.rows.length;
    for (i=0;i<rowNum;i++) {
        tb.deleteRow(i);
        rowNum=rowNum-1;
        i=i-1;
    }
}

function create_file_table(json_data) {
    var t_table = document.getElementById("t_table");
    deltr();
    for (var i=0; i<json_data.length; i++) {
        var tr = document.createElement("tr");
        var td = document.createElement("td");var td1 = document.createElement("td");var td2 = document.createElement("td");var td3 = document.createElement("td");var td4 = document.createElement("td");
        td.innerHTML = i+1;td2.title = i+1;td.style.textAlign = "center";
        td1.innerHTML = json_data[i]["file_name"];td1.title = json_data[i]["file_name"];td1.style.textAlign = "center";
        td2.innerHTML = json_data[i]["file_size"];td2.title = json_data[i]["file_size"];td2.style.textAlign = "center";
        td3.innerHTML = json_data[i]["uploader"];td3.title = json_data[i]["uploader"];td3.style.textAlign = "center";
        td4.innerHTML = "<a target='_blank' href='/"+json_data[i]["file_path"]+"' id='"+json_data[i]["id"]+"'>查看 </a> | <a href='#' id='"+json_data[i]["id"]+"' onclick='Confirm_delete2(this.id)' data-toggle=\"modal\" data-target=\".bd-example-modal-sm\"> 删除</a>";td4.style.textAlign = "center";
        tr.appendChild(td);tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);tr.appendChild(td4);
        t_table.appendChild(tr);
    }
}

function Confirm_delete2(ele_id) {
    document.getElementById("countersign").setAttribute("onclick", "delete_news("+ele_id+")");
    var myArray = new Array();
    var check_box_list = document.getElementsByClassName("check_box");
    for (var t = 0; t < check_box_list.length; t++) {
        if (check_box_list[t].checked) {
            myArray.push(check_box_list[t].id)
        }
    }
    document.getElementById("Confirm-content").innerHTML = "<i style='color: #1E90FF;font-size: 50px;vertical-align:middle;' class=\"iconfont icon-jinggao-chucuo\"></i> <span>确认要删除这此文件吗？</span>"
}

function delete_news(ele_id) {
    document.getElementById("close-model2").click();
    // var page = getCookie("page");
    var page = '1'
    var car_id = document.getElementById("car_id").innerHTML;
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('GET', "/administrative/upload_car_file.html?"+"&delete_list="+String(ele_id)+"&car_id="+String(car_id), true);
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                create_file_table(JsonDate["data"]);
            }
        }
    };
}

function upload_car_file() {
    var form = new FormData();
    var files = document.getElementById("accessory").files[0];
    var car_id = document.getElementById("car_id").innerHTML;
    form.append("file", files);
    form.append("car_id", car_id);

    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('post', "/administrative/upload_car_file.html", true);
    xhr.send(form);
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                create_file_table(JsonDate["data"])
            }
        }
    };
}
